package org.bcncoding.appengine.droiding.client;

import org.bcncoding.appengine.droiding.client.service.LoginService;
import org.bcncoding.appengine.droiding.client.service.LoginServiceAsync;
import org.bcncoding.appengine.droiding.client.widget.ServiceProviderTableWidget;
import org.bcncoding.appengine.droiding.server.pojo.LoginInfo;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DroidingAdmin implements EntryPoint, ResizeHandler {

	private LoginInfo loginInfo = null;

	private VerticalPanel loginPanel = new VerticalPanel();

	private Label loginLabel = new Label(
			"Sign in to your Google Account to access the Droiding application.");

	private Anchor signInLink = new Anchor("Sign In");

	private static final Images images = GWT.create(Images.class);

	public interface Images extends TopPanel.Images {
	}
	
	private TopPanel topPanel = new TopPanel(images);

	private DecoratedTabPanel tabPanel = new DecoratedTabPanel();
		
	private ServiceProviderTableWidget serviceProviders = null;
	
	public void onModuleLoad() {
		LoginServiceAsync loginService = GWT.create(LoginService.class);
		loginService.login(GWT.getHostPageBaseURL(),
				new AsyncCallback<LoginInfo>() {
					public void onFailure(Throwable error) {

					}

					public void onSuccess(LoginInfo result) {
						loginInfo = result;
						if (loginInfo.isLoggedIn()) {
							loadMainPanel();
						} else {
							loadLogin();
						}
					}
				});
	}

	private void loadLogin() {
		signInLink.setHref(loginInfo.getLoginUrl());
		loginPanel.add(loginLabel);
		loginPanel.add(signInLink);
		RootPanel.get().add(loginPanel);
	}

	private void loadMainPanel() {
		VerticalPanel testPanel = new VerticalPanel();
		testPanel.setWidth("100%");
		testPanel.setHeight("100%");		
		serviceProviders = new ServiceProviderTableWidget(10);
		testPanel.add(serviceProviders);

		VerticalPanel centerPanel = new VerticalPanel();
		centerPanel.setWidth("100%");
		centerPanel.setHeight("100%");
		tabPanel.add(centerPanel, "First Panel");
		
		tabPanel.setAnimationEnabled(true);
		tabPanel.setWidth("100%");
		tabPanel.setHeight("100%");
		topPanel.setWidth("100%");
		tabPanel.selectTab(0);
		tabPanel.add(testPanel, "Service Providers");
		DockPanel outer = new DockPanel();
		outer.add(topPanel, DockPanel.NORTH);
		outer.add(tabPanel, DockPanel.CENTER);
		outer.setWidth("100%");
		outer.setSpacing(4);
		Window.addResizeHandler(this);
		Window.enableScrolling(false);
		Window.setMargin("0px");
		RootPanel.get().add(outer);
	}

	public void onResize(ResizeEvent event) {
		// TODO Auto-generated method stub
	}

}
